#include "main.h"

lv_font_t *ft_small;
lv_font_t *ft_medium;
lv_font_t *ft_large;

StatusBar_t StatusBar;

lv_style_t def_style;
lv_style_t parent_style;
lv_style_t opa_style;

// 字体文件
char *Fonts_Path[]={
    FS_PREFIX"/Fonts/msyh.ttc", // 常规
    FS_PREFIX"/Fonts/msyhl.ttc", // 细
    FS_PREFIX"/Fonts/msyhbd.ttc", // 粗
};
// static void setting_show(lv_event_t * e);
// static void setting_close(lv_event_t * e);

static void timelabel_cb (lv_timer_t *timer)
{
    static char timebuffer[50];
    reemoon_get_system_time(timebuffer, sizeof(timebuffer));
    lv_label_set_text (StatusBar.time, timebuffer);
}

void TOP_StatusBar_Init(lv_obj_t * parent)
{
    StatusBar.top_bar = lv_obj_create (parent);
    lv_obj_t * Status_Bar = StatusBar.top_bar;
    lv_obj_clear_flag(Status_Bar, LV_OBJ_FLAG_SCROLLABLE);
    if (system_info->font == FT_SMALL)
        StatusBar.bar_h = 32;
    else if (system_info->font == FT_MEDIUM)
        StatusBar.bar_h = 40;
    else 
        StatusBar.bar_h = 47;
    lv_obj_set_size (Status_Bar, SCREEN_INFO.DISPLAY_W, LV_SIZE_CONTENT);
    lv_obj_set_align(Status_Bar, LV_ALIGN_TOP_MID);
    // rm_logo
    lv_obj_t *rm_logo_t = lv_img_create(Status_Bar);
    lv_img_set_src(rm_logo_t, "A:"FS_PREFIX"/src/img/MyLogo40201.png");//&rm_logo);
    lv_obj_set_align(rm_logo_t, LV_ALIGN_TOP_LEFT);
    // 时间
    StatusBar.time = lv_label_create (Status_Bar);
    lv_obj_t *time_label = StatusBar.time;
    lv_obj_set_size (time_label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_obj_set_align(time_label, LV_ALIGN_TOP_MID);
    char timebuffer[50];
    reemoon_get_system_time(timebuffer, sizeof(timebuffer));
    lv_label_set_text (StatusBar.time, timebuffer);
    // 定时器刷新时间
    StatusBar.timer = lv_timer_create(timelabel_cb, 1000, NULL);

    // 标题设置
    StatusBar.title = lv_label_create(Status_Bar);
    lv_obj_t *title_label = StatusBar.title;
    lv_obj_set_size (title_label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_label_set_text(title_label, ProjectName);
    lv_obj_set_align(title_label, LV_ALIGN_TOP_RIGHT);
}
static void network_status_change(lv_event_t *e)
{
    if(e->code == LV_EVENT_FLUSH_START)
    {
        if (RABBIT_STAUTS==false)
            lv_obj_set_style_bg_color(StatusBar.network_status, lv_palette_main(LV_PALETTE_RED), 0);
        else
            lv_obj_set_style_bg_color(StatusBar.network_status, lv_palette_main(LV_PALETTE_GREEN), 0);
    }
}
void BTM_StatusBar_Init(lv_obj_t * parent)
{
    StatusBar.btm_bar = lv_obj_create(parent);
    lv_obj_t *Status_Bar = StatusBar.btm_bar;
    lv_obj_clear_flag(Status_Bar, LV_OBJ_FLAG_SCROLLABLE);// 禁用滚轮
    lv_obj_set_size(Status_Bar, SCREEN_INFO.DISPLAY_W, StatusBar.bar_h);
    lv_obj_set_align(Status_Bar, LV_ALIGN_BOTTOM_MID);
    // 版本信息
    StatusBar.version = lv_label_create(Status_Bar);
    lv_obj_t *ver_label = StatusBar.version;
    lv_obj_add_style(ver_label, &opa_style, 0);
    lv_obj_set_size(ver_label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    if (strlen(system_info->version) == 0)
        lv_label_set_text(ver_label, RM_VERSION);
    else
        lv_label_set_text(ver_label, system_info->version);
    lv_obj_set_align (ver_label, LV_ALIGN_RIGHT_MID);
    // 小绿点 
    StatusBar.network_status= lv_obj_create(Status_Bar);
    lv_obj_t *network_status = StatusBar.network_status;
    lv_obj_clear_flag(network_status, LV_OBJ_FLAG_SCROLLABLE);// 禁用滚轮
    lv_obj_set_size(network_status, 30, 30);
    lv_obj_set_style_radius(network_status, 30/2, 0);
    lv_obj_set_align (network_status, LV_ALIGN_LEFT_MID);
    lv_obj_set_style_bg_color(StatusBar.network_status, lv_palette_main(LV_PALETTE_RED), 0);
    lv_obj_add_event_cb (network_status, network_status_change, LV_EVENT_FLUSH_START, NULL);
    // 系统设置
    lv_obj_t *setting = lv_button_create (Status_Bar);
    lv_obj_set_size(setting, LV_DPX(150), LV_SIZE_CONTENT);
    lv_obj_add_flag(setting, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_t * label = lv_label_create (setting);
    lv_label_set_text(label, SETTING);
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_set_align(setting, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(setting, setting_show, LV_EVENT_CLICKED, parent);
}

// 父页初始化
lv_obj_t *UI_Init(void)
{
    // 0. 初始化字体
	char *pathname = Fonts_Path[2];
	ft_small  = lv_freetype_font_create (pathname, LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 24, LV_FREETYPE_FONT_STYLE_NORMAL);
	ft_medium = lv_freetype_font_create (pathname, LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 32, LV_FREETYPE_FONT_STYLE_NORMAL);
    ft_large  = lv_freetype_font_create (pathname, LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 48, LV_FREETYPE_FONT_STYLE_NORMAL);

    if (system_info->font == FT_SMALL)
        system_info->sysfont = ft_small;
    else if (system_info->font == FT_MEDIUM)
        system_info->sysfont = ft_medium;
    else if (system_info->font == FT_LARGE)
        system_info->sysfont = ft_large;
    else
        system_info->sysfont = ft_medium;

    if (ft_small == NULL || ft_medium == NULL || ft_large == NULL)
    {
        reemoon_error ("fonts file not find\n");
        return NULL;
    }

    // 设置父页面 flex布局 样式
    lv_style_init(&parent_style);
    // lv_style_set_layout(&parent_style, LV_LAYOUT_FLEX); // flex布局
    lv_style_set_flex_flow(&parent_style, LV_FLEX_FLOW_COLUMN);
#if LV_USE_SDL
    lv_style_set_text_font(&parent_style, ft_small); // 字体
#else
    lv_style_set_text_font(&parent_style, system_info->sysfont); // 字体
#endif
    lv_style_init(&opa_style);
    lv_style_set_opa(&opa_style, LV_OPA_50);

    // 1. 创建父页面
    lv_obj_t *parent_screen = lv_obj_create(lv_screen_active());
	lv_palette_t main_palette = LV_PALETTE_TEAL;
	lv_theme_default_init(NULL, lv_palette_main(main_palette), lv_palette_main(main_palette+3), LV_THEME_DEFAULT_DARK, system_info->sysfont);
    lv_obj_add_style(parent_screen, &parent_style, 0);
    lv_obj_set_size(parent_screen, LV_PCT(100), LV_PCT(100));
    lv_obj_set_flex_flow (parent_screen, LV_FLEX_FLOW_COLUMN);

    lv_obj_set_style_pad_all (parent_screen, 0, 0);
    lv_obj_set_style_border_side (parent_screen, 0, 0);
    lv_obj_set_flex_grow (parent_screen, 0);

    // 创建顶部状态栏
    TOP_StatusBar_Init(parent_screen);
    // 创建出口信息页面
    exitInof_PageInit(parent_screen, SCREEN_INFO.DISPLAY_W, SCREEN_INFO.DISPLAY_H-StatusBar.bar_h*2.35);
    // 创建底部状态栏
    BTM_StatusBar_Init(parent_screen);

    // if (parent_screen != NULL)
    //     lv_scr_load(parent_screen);
    // else
    //     reemoon_error ("parent page not exist\n");

    return parent_screen;
}
